﻿/**
@file
    ServiceContents.cs
@brief
    Copyright 2009 Creative Crew. All rights reserved.
@author
    William Chang
@version
    0.1
@date
    - Created: 2009-09-09
    - Modified: 2009-03-20
    .
@note
    References:
    - General:
        - http://encosia.com/2008/03/27/using-jquery-to-consume-aspnet-json-web-services/
        - http://www.asp.net/learn/ajax/tutorial-03-cs.aspx
        - http://www.dev102.com/2008/04/30/call-aspnet-webmethod-from-jquery/
        .
    .
*/

using System;
using System.Web.Services;

namespace ent {

/// <summary>Class ServiceContents extends ServiceCommon</summary>
[System.Web.Script.Services.ScriptService()]
public class ServiceContents : ServiceCommon {
    /// <summary>Default constructor.</summary>
    public ServiceContents() {}
    /// <summary>Get a RSS feed.</summary>
    /// <returns>A System.Data.DataSet instance containing the parsed xml.</returns>
    public System.Data.DataSet getFeed(String urlFeed) {
        System.Xml.XmlTextReader xmlReader = new System.Xml.XmlTextReader(urlFeed);
        System.Data.DataSet ds1 = new System.Data.DataSet("feed");

        ds1.ReadXml(xmlReader);
        return ds1;
    }
    /// <summary>Get posts by type, parent id, assigned to (from session), and is trashed.</summary>
    /// <remarks>User must have authenticate session to proceed.</remarks>
    [System.Web.Services.WebMethod(EnableSession = true)]
    public String getContentsByTypeAndParentAndAssignedToAndTrashed(int intType, int intParentId, int intTrashed) {
        // Authenticate.
        if(!ApplicationCommon.isValidSession(Session)) {return String.Empty;}

        TableContents t2 = new TableContents();
        System.Collections.Hashtable p1 = new System.Collections.Hashtable();
        int postAssignedTo = TableLists.getAssignedToByRole(Session);
        
        // Use custome view.
        p1.Add("option_select", 1);
        // Filter by deleted (trashed).
        p1.Add(TableContents.TBL__contents__content_deleted, intTrashed);
        // Filter by type.
        p1.Add(TableContents.TBL__contents__content_type, intType);
        // Filter by parent.
        if(intParentId > -1) {
            p1.Add(TableContents.TBL__contents__content_parent_id, intParentId);
        }
        // Filter by assigned to (from session).
        switch(postAssignedTo) {
            case (int)TableLists.enumContentAssignedTo.Moderators:
                p1.Add(TableContents.TBL__contents__content_assignedto, postAssignedTo);
                break;
            case (int)TableLists.enumContentAssignedTo.Authors:
                p1.Add(TableContents.TBL__contents__content_assignedto, postAssignedTo);
                p1.Add("option__content_assignedto___session_user_id", Session[ApplicationCommon.SESSION__user_id].ToString());
                break;
        }

        System.Data.DataTable dt1 = t2.getContents(p1);
        dt1.TableName = TableContents.TBL__contents;
        return DatabaseCommon.toJson(dt1);
    }
    /// <summary>Get content.</summary>
    /// <returns>A System.Data.DataRow instance containing the matched row.</returns>
    public System.Data.DataRow getContent(int id) {
        TableContents t2 = new TableContents();
        return t2.getContent(id);
    }
    /// <summary>Get contents extended columns (passing integer zero as an argument gets all).</summary>
    /// <returns>A System.Data.DataTable instance containing the matching rows.</returns>
    public System.Data.DataTable getContentsExtended(int id) {
        TableContents t2 = new TableContents();
        System.Collections.Hashtable p1 = new System.Collections.Hashtable();

        p1.Add(TableContents.TBL__contents_hierarchy__content_id, id);
        return t2.getContents(p1);
    }
    /// <summary>Get contents extended columns.</summary>
    /// <returns>A System.Data.DataTable instance containing the matching rows.</returns>
    public System.Data.DataTable getContentsExtendedBySlug(String slug) {
        TableContents t2 = new TableContents();
        System.Collections.Hashtable p1 = new System.Collections.Hashtable();

        p1.Add(TableContents.TBL__contents_hierarchy__content_title_slug, slug);
        return t2.getContents(p1);
    }
    /// <summary>Get contents by parent id (passing integer zero as an argument gets all).</summary>
    /// <returns>A System.Data.DataTable instance containing the matching rows.</returns>
    public System.Data.DataTable getContentsByParent(int idParent) {
        TableContents t2 = new TableContents();
        System.Collections.Hashtable p1 = new System.Collections.Hashtable();

        p1.Add(TableContents.TBL__contents__content_parent_id, idParent);
        p1.Add("option__content_parent_id___include_parent", 0);
        return t2.getContents(p1);
    }
    /// <summary>Get recent contents.</summary>
    /// <returns>A System.Data.DataTable instance containing the matching rows.</returns>
    public System.Data.DataTable getContentsRecentAndAssignedTo(int itemsLimit, System.Web.SessionState.HttpSessionState userSession) {
        TableContents t2 = new TableContents();
        System.Collections.Hashtable p1 = new System.Collections.Hashtable();

        int postAssignedTo = TableLists.getAssignedToByRole(Session);

        // Filter by assigned to (from session).
        switch(postAssignedTo) {
            case (int)TableLists.enumContentAssignedTo.Moderators:
                p1.Add(TableContents.TBL__contents__content_assignedto, postAssignedTo);
                break;
            case (int)TableLists.enumContentAssignedTo.Authors:
                p1.Add(TableContents.TBL__contents__content_assignedto, postAssignedTo);
                p1.Add("option__content_assignedto___session_user_id", Session[ApplicationCommon.SESSION__user_id].ToString());
                break;
        }

        p1.Add("limit", itemsLimit);
        return t2.getContentsRecent(p1);
    }
    /// <summary>Set content.</summary>
    /// <remarks>User must have authenticate session to proceed.</remarks>
    [System.Web.Services.WebMethod(EnableSession = true)]
    public bool setContent(int intContentId, int intUserId, String strTitle, String strBody, int intOrder) {
        // Authenticate.
        if(!ApplicationCommon.isValidSession(Session)) {return false;}

        TableContents t2 = new TableContents();
        System.Collections.Hashtable p1 = new System.Collections.Hashtable();

        p1.Add(TableContents.TBL__contents__content_id, intContentId);
        p1.Add(TableContents.TBL__contents__content_editor_user_id, intUserId);
        p1.Add(TableContents.TBL__contents__content_title, DatabaseCommon.trimAll(strTitle));
        p1.Add(TableContents.TBL__contents__content_title_slug, TableContents.toSlug(strTitle));
        p1.Add(TableContents.TBL__contents__content_body, strBody);
        p1.Add(TableContents.TBL__contents__content_order, intOrder);
        p1.Add(TableContents.TBL__contents__content_date_modified, DateTime.Now);
        return t2.setContent_Ajax(p1);
    }
    /// <summary>Get content (passing zero gets all).</summary>
    /// <returns>A (stringify JSON) String instance containing the matching rows.</returns>
    [System.Web.Services.WebMethod()]
    public String getJsonContent(int id) {
        TableContents t2 = new TableContents();
        System.Collections.Hashtable p1 = new System.Collections.Hashtable();

        p1.Add(TableContents.TBL__contents__content_id, id);
        return DatabaseCommon.toJson(t2.getContents(p1));
    }
    /// <summary>Remove content.</summary>
    /// <remarks>User must have authenticate session to proceed.</remarks>
    [System.Web.Services.WebMethod(EnableSession = true)]
    public bool removeContent(int intId) {
        // Authenticate.
        if(!ApplicationCommon.isValidSession(Session)) {return false;}

        TableContents t2 = new TableContents();
        return t2.dynamicSqlDelete(TableContents.TBL__contents, TableContents.TBL__contents__content_id + " = " + intId);
    }
    /// <summary>Restore content.</summary>
    /// <remarks>User must have authenticate session to proceed.</remarks>
    [System.Web.Services.WebMethod(EnableSession = true)]
    public bool restoreContent(int intId) {
        // Authenticate.
        if(!ApplicationCommon.isValidSession(Session)) {return false;}

        TableContents t2 = new TableContents();
        System.Collections.Hashtable p1 = new System.Collections.Hashtable();

        p1.Add(TableContents.TBL__contents__content_deleted, 0);
        return t2.dynamicSqlUpdate(p1, TableContents.TBL__contents, TableContents.TBL__contents__content_id + " = " + intId);
    }
    /// <summary>Trash content.</summary>
    /// <remarks>User must have authenticate session to proceed.</remarks>
    [System.Web.Services.WebMethod(EnableSession = true)]
    public bool trashContent(int intId) {
        // Authenticate.
        if(!ApplicationCommon.isValidSession(Session)) {return false;}

        TableContents t2 = new TableContents();
        System.Collections.Hashtable p1 = new System.Collections.Hashtable();

        p1.Add(TableContents.TBL__contents__content_deleted, 1);
        return t2.dynamicSqlUpdate(p1, TableContents.TBL__contents, TableContents.TBL__contents__content_id + " = " + intId);
    }
}

} // END namespace ent